
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rmi;

import java.math.*;

/**
 *
 * @author TealeaF
 */
public class CalculDePi {
    
    private int debut;
    private int fin;
    private int nbDecimal;

    public CalculDePi(int debut, int fin, int nbDecimal) {
        this.debut = debut;
        this.fin = fin;
        this.nbDecimal = nbDecimal;
    }
    
    
    
    
    public BigDecimal calculIntervalle(int debutInter, int finInter) {
        
        System.out.println("debut = " + debut + " fin = " + fin);
        
        BigDecimal somme = BigDecimal.ZERO;
        for (int j = debutInter; j < finInter; j++) {
            BigDecimal part1 = new BigDecimal(4.0 / (8 * j + 1));
            BigDecimal part2 = new BigDecimal(2.0 / (8 * j + 4));
            BigDecimal part3 = new BigDecimal(1.0 / (8 * j + 5));
            BigDecimal part4 = new BigDecimal(1.0 / (8 * j + 6));
            BigInteger part5 = BigInteger.valueOf(16).pow(j);
            part1 = part1.subtract(part2);
            part1 = part1.subtract(part3);
            part1 = part1.subtract(part4);
            part1 = part1.divide(new BigDecimal(part5));
            
            somme = somme.add(part1);
        }
        return (somme);
    }
    
    public BigDecimal calcul() {
        
        int maxPalier = 10;
        
        BigDecimal somme;
        somme = calculIntervalle(debut, fin);
//        somme = calculIntervalle(debut, debut+19);
//        for (int i = debut; i < fin; i++) {
//            somme = somme.add(calculIntervalle(i * 20, (i + 1) * 20));
//        }
        BigDecimal nbd = new BigDecimal(10.0).pow(nbDecimal);
//        System.out.println("nbDecimal = " + nbDecimal);
//        System.out.println("nbd = " + nbd);
//        System.out.println("somme = " + somme);
        somme = somme.multiply(nbd);
        BigInteger tempSomme = somme.toBigInteger();
//        System.out.println("temp = " + tempSomme);
        return (new BigDecimal(tempSomme).divide(nbd));
        
    }
    
    public BigDecimal calcul2(int nbDecimal) {
        
        BigDecimal somme;
        somme = calculIntervalle(0, 20);
        somme = somme.add(calculIntervalle(20, 40));
//        for (int i = debut; i < fin; i++) {
//            somme = somme.add(calculIntervalle(i * 20, (i + 1) * 20));
//        }
        BigDecimal nbd = new BigDecimal(10.0).pow(nbDecimal);
        System.out.println("nbDecimal = " + nbDecimal);
        System.out.println("nbd = " + nbd);
        System.out.println("somme = " + somme);
        somme = somme.multiply(nbd);
        BigInteger tempSomme = somme.toBigInteger();
        System.out.println("temp = " + tempSomme);
        return (new BigDecimal(tempSomme).divide(nbd));
        
    }
    
    public static void main(String args[]) {
       CalculDePi c = new CalculDePi(0, 20, 40);
       c.calcul();
       c = new CalculDePi(20, 40, 40);
       c.calcul();
       System.out.println("");
       System.out.println("");
       System.out.println("");
       BigDecimal s = c.calcul2(40);
       System.out.println("valeur : " + s.toString());
        System.out.println("decimal : " + (s.toString().length() - 2));
    }
    
//    public BigDecimal calcul() {
////        CalculDePi cdpi = new CalculDePi();
////        BigDecimal s = calcul(fin - debut);
//        BigDecimal s = calcul();
////        BigDecimal s = calculIntervalle(debut, fin);
//        System.out.println("valeur : " + s.toString());
//        System.out.println("decimal : " + (s.toString().length() - 2));
//        return s;
////        decoupeAffichage(s);
//        
//    }
//    
    public static void decoupeAffichage (String s){
        while (s.length() > 4000) {
            System.out.println(s.substring(0, 4000));
            s = s.substring(4000, s.length());
        }
        System.out.println(s);
    }
}
